import { defineStore } from "pinia";
import indexApi from '@/api/home'
import type { BigPromotionData, CategoryHotSellData, FocusListData, IndexActivityData, KingKongListData, PolicyDescListData, SceneLightShoppingGuideData } from '@/api/home';
import { ElMessage } from "element-plus";
// import type {IndexInfoData} from '@/api/home';

// 热销榜
function initCategory(){
  return {
    title: '',
    categoryList: []
  }
}
// 新人专享礼
function initIndexActivity(){
  return {
    activityPrice: '',
    originPrice: '',
    picUrl: '',
    showPicUrl: '',
    subTitle: '',
    targetUrl: '',
    title: ''
  }
}
// 分类列表
function initKingKong(){
  return {
    picUrl: '',
    text: ''
  }
}
// adv广告
function initPolicyDesc(){
  return {
    desc: '',
    icon: ''
  }
}
// sales促销
function initBigPromotion(){
  return {
    floorList: []
  }
}

type HomeStoreStateData = {
  focusList: FocusListData,
  categoryHotSellModule: CategoryHotSellData,
  indexActivityModule: IndexActivityData, //新人专享礼
  kingKongModule: KingKongListData, //分类列表
  policyDescList: PolicyDescListData, //adv广告
  sceneLightShoppingGuideModule: SceneLightShoppingGuideData, // main底部
  bigPromotionModule: BigPromotionData //促销
}

export const useHomeStore = defineStore("home", {
  state:() => <HomeStoreStateData> {
      focusList: [], //轮播图
      categoryHotSellModule: initCategory(),// 类目热销榜
      indexActivityModule: initIndexActivity(), //新人专享礼
      kingKongModule: initKingKong(), //分类列表
      policyDescList: initPolicyDesc(), //adv广告
      sceneLightShoppingGuideModule: [], // main底部
      bigPromotionModule: initBigPromotion()  //促销
  },
  actions: {
    // 推荐页
    async getIndex(){
      try {
        const result = await indexApi.getIndex();
        // 轮播图
        this.focusList = result.data.focusList;
        // 类目热销榜
        this.categoryHotSellModule = result.data.categoryHotSellModule;
        // 新人专享礼
        this.indexActivityModule = result.data.indexActivityModule;
        // 分类列表
        this.kingKongModule = result.data.kingKongModule;
        // adv广告
        this.policyDescList = result.data.policyDescList;
        // main底部
        this.sceneLightShoppingGuideModule = result.data.sceneLightShoppingGuideModule;
        // 促销
        this.bigPromotionModule = result.data.bigPromotionModule;
        
      } catch (error) {
        ElMessage.error('获取数据失败');
      }
    }
  },
  getters: {
    // 类目热销榜数据
    bigList(state){
      return state.categoryHotSellModule.categoryList.slice(0,2);
    },
    hotList(state){
      return state.categoryHotSellModule.categoryList.slice(2);
    },
    // main底部
    banner(state){
      return (state.sceneLightShoppingGuideModule[0] || {}).styleBanner || {}
    },
    itemList(state){
      return (state.sceneLightShoppingGuideModule[1] || {}).styleItem || {}
    },
    // 促销
    floorList(state){
      return ((state.bigPromotionModule || {}).floorList[0] || []).cells || []
    }
  }
});
